﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using BIZ;
using System.Collections;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
using DomainModel;

namespace joyAdmin.Controllers
{
    [DomainConfirmationFilter]
    [BIZ.AdminFilter]
    public class cmsController : Controller
    {
        sectionsOper sectionsoper = new sectionsOper();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult head()
        {
            return View();
        }
        public JsonResult loginout()
        {
            Session["cmsuser"] = null;
            return Json("y");
        }

        public ActionResult main()
        {
            return View();
        }


        public ActionResult left()
        {
            ViewData["sectionleft"] = sectionsoper.getList();           
            return View();
        }

        public ActionResult sectionlist()
        {
            string pid = Request.QueryString["pid"];
            IList list;
            if (!String.IsNullOrEmpty(pid))
                list =sectionsoper.getListByPId(Convert.ToInt16(pid));
            else
                list = sectionsoper.getList();
            ViewData["sectionlist"] = list;
            return View("sectionlist");
        }

        [ValidateInput(false)]
        public JsonResult SaveSections()
        {
            sections _sections = new JavaScriptSerializer().Deserialize<sections>(Request.Form["sections"].ToString());
            if (Regex.IsMatch(_sections.displaysql, "delete|update", RegexOptions.IgnoreCase))
                return Json("error=包含危险代码！");
            if (_sections.id != 0)
            {
                _sections.addTime = _sections.addTime.ToLocalTime();
                _sections.lastOid = ((DomainModel.cmsuser)Session["cmsuser"]).id;
                _sections.lastTime = DateTime.Now;
                _sections.addTime = _sections.addTime.ToLocalTime();
            }
            else
            {
                _sections.addTime = DateTime.Now;
                _sections.csid = ((DomainModel.cmsuser)Session["cmsuser"]).id;
            }
            sectionsoper.sava(_sections);
            IList list = sectionsoper.getList();
            ViewData["sectionlist"] = list;
            return Json("success");
        }
        public ActionResult DelSection()
        {
            string id = Request.Form["id"];
            sectionsoper.deleteFromSections(Convert.ToInt32(id));
            IList list = sectionsoper.getList();
            ViewData["sectionlist"] = list;
            return View("sectionlist");
        }
        public ActionResult checkCode()
        {
            string code = Request.Form["code"];
            int id = Convert.ToInt16(Request.Form["id"]);
            int res = 0;
            res = sectionsoper.checkcode(code.ToString(), id);
            if (res != 0)
            {
                return Json("error");
            }
            return Json("ok");
        }


        public JsonResult getSectionById()
        {
            string id = Request.Form["id"];
            return Json(sectionsoper.getSectionById(id)[0]);

        }

    }
}
